layui.use(['form','layer','table','laytpl'],function() {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    // 检验类型列表

    var tableIns = table.render({
        elem: '#userList',
        url : 'http://101.201.56.140:10086/pc/InspectType/selAll',
        crossDomain:true,
        request:{
            statusName:'status',
            pageName:'pageNum',
            limitName: 'pageSize'
        },
        where:{
            "inspectTypeClass": "string",
            "inspectTypeDescribe": "string",
            "inspectTypeName": "string",
            "pageNum": 0,
            "pageSize": 10,
            "status": "A"
        },
        method:'post',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'inspectTypeID',title: '检验类型ID',align: 'center',width: 100},
            {field: 'inspectTypeName', title: '检验类型名称', minWidth:100, align:"center"},
            {field: 'inspectTypeDescribe', title: '检验类型描述', align:'center'},

            {
                field: 'inspectTypeClass', title: '检验分类',width:100, align: 'center', templet: function (d) {
                    if (d.inspectTypeClass == "0") {
                        return "质量";
                    } else if (d.inspectTypeClass == "1") {
                        return "安全";

                    }
                }
            },
            {field: 'inspectTypeTime', title: '创建时间', align:'center',minWidth:150},
            {title: '操作', minWidth:175, templet:'#userListBar',fixed:"right",align:"center"},

        ]],
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").click(function () {
        var inputVal = $('.layui-input ').val();

        table.reload('userListTable', {

            url: 'http://101.201.56.140:10086/pc/InspectType/selByID',
            method: 'post',
            request: {
                statusName: 'status',
                // pageName:'pageNum',
                // limitName: 'pageSize'
            },
            where: {
                inspectTypeID: inputVal,
                status: 0

            },
            page: {
                curr: 1
            }

        })
    })




    //添加用户
    function addUser(edit) {
        var index = layui.layer.open({
            title: "添加类型",
            type: 2,
            content: "../dataAdd/TypeAdd.html",
            url:'http://localhost:10086/InspectType/add',
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                if (edit) {
                    body.find(".testType").val(edit.testType);  //检验类型名称
                    body.find(".testClassify").val(edit.testClassify);  //检验分类
                    body.find(".testDepict").text(edit.testDepict);    //检验描述
                    form.render();
                }
                setTimeout(function () {
                    layui.layer.tips('点击此处返回类型列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index", index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }

    $(".addNews_btn").click(function () {
        addUser();
    })

    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('userListTable'),
            data = checkStatus.data,
            newsId = [];
        if (data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].newsId);
            }
            layer.confirm('确定删除选中的类型？', {icon: 3, title: '提示信息'}, function (index) {
                $.post("http://localhost:10086//pc/InspectType/delete",{
                    newsId : newsId  //将需要删除的newsId作为参数传入
                },function(data) {
                    if (data.code == 200) {
                        tableIns.reload();
                    }
                    layer.msg(data.msg);
                })
                //
                layer.close(index);
            })

        } else {
            layer.msg("请选择需要删除的类型");
        }
        layer.close()
    })

    //列表操作
    table.on('tool(userList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;

        if (obj.event === 'edit') { //编辑
            addUser(data)

        } else if (obj.event === 'del') { //删除
            layer.confirm('确定删除此类型？', {icon: 3, title: '提示信息'}, function (index) {
                $.post({
                    datatype: 'json',
                    url: 'http://101.201.56.140:10086/pc/InspectType/delete',
                    data: {'inspectTypeID': data.inspectTypeID},

                }, function (data) {
                    obj.del();
                    layer.close(index);
                    // })
                });
            })
        }


    })

})